<?php 
    class Districts extends MX_Controller
    {
        function __construct() {
            parent::__construct();
            $this->load->library('pagination');  
        }
        
        function index($row=0)
        {
            if(!($this->session->userdata('login'))) redirect('administrator/login');
            //Check Quyền
            //if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
            //redirect('administrator/index');
                        
            if($this->input->post('txtname'))
            {
                //add Term
                $district = new District();
                $district->name = $this->input->post('txtname');
                $district->code = $this->input->post('txtcode');
                $district->province_id = $this->input->post('ddlProvince');
                
                $district->save();
            }
            
            $district = new District();            
            
            include('paging.php');
            $config['base_url']= base_url()."administrator/districts/index/";
            $config['total_rows'] = $district->count();
            $config['cur_page']= $row;
            $config['num_links'] = 3;		
            $this->pagination->initialize($config);
            $data['list_link'] = $this->pagination->create_links();	
                      
            $data['lstDistrict'] = $district->limit($config['per_page'], $row)->get();
            
            $province = new Province();
            $data['lstProvince'] = $province->get(); 
            $data['view'] = 'districts_index';
            $this->load->view('back_end/template_noright',$data);
        }
        
        function edit($id,$row=0)
        {
            if(!($this->session->userdata('login'))) redirect('administrator/login');
            //Check Quyền
            //if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
            //redirect('administrator/index');
                        
            if($this->input->post('txtname'))
            {
                $id = $this->input->post('district_id');
                
                $district = new District();
                $district->where('id',$id)->get();
                $district->name = $this->input->post('txtname');
                $district->code = $this->input->post('txtcode');
                $district->province_id = $this->input->post('ddlProvince');
                
                $district->save();
                redirect('administrator/districts');
            }
            
            $district = new District();            
            
            include('paging.php');
            $config['base_url']= base_url()."administrator/districts/edit/".$id.'/';
            $config['total_rows'] = $district->count();
            $config['cur_page']= $row;
            $config['num_links'] = 3;		
            $this->pagination->initialize($config);
            $data['list_link'] = $this->pagination->create_links();	
                      
            $data['lstDistrict'] = $district->limit($config['per_page'], $row)->get();
            
            $province = new Province();
            $data['lstProvince'] = $province->get(); 
            $district = new District();
            $data['district'] = $district->where('id',$id)->get();
            $data['view'] = 'districts_edit';
            $this->load->view('back_end/template_noright',$data);
        }
        
        function delete()
        {
            $id = $this->input->post('param');
            $district = new District();
            $district->where('id',$id)->get();
            $district->delete();
        }
    }
?>  